/*
 *      set.c
 *      
 *      Copyright 2009 Daniele Ricci <denn86@gmail.com>, Luca Saccagi <luksak86@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _SET_IMPL
#define _SET_IMPL

#include "idmp.h"

#ifdef _WIN32 
static void setMessage(AlertList *l,xmlNode *root){
#else
void setMessage(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"Alert",6)==0)&&(son->children!=NULL)){
			setAlert(l, son);
			MSGcnt++;
			/*DBG*/DEBUG != DAEMON ? resProg("",MSGcnt) : 0;
		}
	}
}

#ifdef _WIN32 
static void setAlert(AlertList *l,xmlNode *root){
#else
void setAlert(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	
	/* Updating Xpath generic filter xmlnode list */
	xpathNodeList = l;

	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"Analyzer",9)==0)&&(son->children!=NULL)){
			setAnalyzer(l, son);
		}
		if((strncmp((char *)son->name,"CreateTime",11)==0)&&(son->children!=NULL)){
			setTime(l, son);
		}
		if((strncmp((char *)son->name,"DetectTime",11)==0)&&(son->children!=NULL)){
			setTime(l, son);
		}
		if((strncmp((char *)son->name,"AnalyzerTime",13)==0)&&(son->children!=NULL)){
			setTime(l, son);
		}
		if((strncmp((char *)son->name,"Source",7)==0)&&(son->children!=NULL)){
			setSource(l, son);
		}
		if((strncmp((char *)son->name,"Target",7)==0)&&(son->children!=NULL)){
			setTarget(l, son);
		}
		if((strncmp((char *)son->name,"Classification",15)==0)&&(son->children!=NULL)){
			setClassification(l, son);
		}
		if((strncmp((char *)son->name,"Assessment",11)==0)&&(son->children!=NULL)){
			setAssessment(l, son);
		}
		if((strncmp((char *)son->name,"ToolAlert",10)==0)&&(son->children!=NULL)){
			setToolAlert(l, son);
		}
		if((strncmp((char *)son->name,"OverflowAlert",14)==0)&&(son->children!=NULL)){
			setOvrfAlert(l, son);
		}
		if((strncmp((char *)son->name,"CorrelationAlert",17)==0)&&(son->children!=NULL)){
			setCorrAlert(l, son);
		}
		if((strncmp((char *)son->name,"AdditionalData",15)==0)&&(son->children!=NULL)){
			setAdditionalData(l, son);
		}
	}
}

#ifdef _WIN32 
static void setAnalyzer(AlertList *l,xmlNode *root){
#else
void setAnalyzer(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"Node",5)==0)&&(son->children!=NULL)){
			setNode(l, son);
		}
		if((strncmp((char *)son->name,"Process",8)==0)&&(son->children!=NULL)){
			setProcess(l, son);
		}
		if((strncmp((char *)son->name,"Analyzer",9)==0)&&(son->children!=NULL)){
			setAnalyzer(l, son);
		}
	}
}
#ifdef _WIN32 
static void setNode(AlertList *l,xmlNode *root){
#else
void setNode(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	xmlAttr *prop;
	int is_ipv4_cat=0;
	
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"location",9)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"name",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				if(son->CONTENT!=NULL){
				//insert into index struct
			}
			}
		}
		if((strncmp((char *)son->name,"Address",8)==0)&&(son->children!=NULL)){
			/* IPV4-Addr check before loading */
       			is_ipv4_cat=0;
        		prop=son->properties;
        		while(prop!=NULL){
                	if((strncmp((char *)prop->name,"category",8)==0)&&(strncmp((char *)prop->CONTENT,"ipv4-addr",9)==0)) is_ipv4_cat=1; 
					/*DBG*/DBG_REALLOC_S ? printf("[%s][%s]\n",(char *)prop->name,(char *)prop->CONTENT) : 0;
		              prop=prop->next;
        		}
			if(is_ipv4_cat == 1) setAddress(l, son);
		}
	}
}

#ifdef _WIN32 
static void setProcess(AlertList *l,xmlNode *root){
#else
void setProcess(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"name",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"pid",4)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"path",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"arg",4)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"env",4)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		
	}
}

#ifdef _WIN32 
static void setSource(AlertList *l,xmlNode *root){
#else
void setSource(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"Node",5)==0)&&(son->children!=NULL)){
			setNode(l, son);
		}
		if((strncmp((char *)son->name,"User",5)==0)&&(son->children!=NULL)){
			setUser(l, son);
		}
		if((strncmp((char *)son->name,"Process",8)==0)&&(son->children!=NULL)){
			setProcess(l, son);
		}
		if((strncmp((char *)son->name,"Service",8)==0)&&(son->children!=NULL)){
			setService(l, son);
		}
	}
}

#ifdef _WIN32 
static void setUser(AlertList *l,xmlNode *root){
#else
void setUser(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"UserId",7)==0)&&(son->children!=NULL)){
			setUserId(l, son);
		}
	}
}

#ifdef _WIN32 
static void setUserId(AlertList *l,xmlNode *root){
#else
void setUserId(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"name",7)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"number",7)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
	}
}


#ifdef _WIN32 
static void setService(AlertList *l,xmlNode *root){
#else
void setService(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"name",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"port",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"portlist",9)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"protocol",9)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"SNMPService",12)==0)&&(son->children!=NULL)){
			//call to SNMPService index func
		}
		if((strncmp((char *)son->name,"WebService",11)==0)&&(son->children!=NULL)){
			//call to WEBService index func
		}
	}
}
#ifdef _WIN32 
static void setTarget(AlertList *l,xmlNode *root){
#else
void setTarget(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"Node",5)==0)&&(son->children!=NULL)){
			setNode(l, son);
		}
		if((strncmp((char *)son->name,"User",5)==0)&&(son->children!=NULL)){
			setUser(l, son);
		}
		if((strncmp((char *)son->name,"Process",8)==0)&&(son->children!=NULL)){
			setProcess(l, son);
		}
		if((strncmp((char *)son->name,"Service",8)==0)&&(son->children!=NULL)){
			setService(l, son);
		}
		if((strncmp((char *)son->name,"File",5)==0)&&(son->children!=NULL)){
			setFile(l, son);
		}
	}
}
#ifdef _WIN32 
static void setFile(AlertList *l,xmlNode *root){
#else
void setFile(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"name",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"path",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"create-time",12)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"modify-time",12)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"access-time",12)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"data-size",10)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"disk-size",10)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"FileAccess",11)==0)&&(son->children!=NULL)){
			//insert into index struct
		}
		if((strncmp((char *)son->name,"Linkage",8)==0)&&(son->children!=NULL)){
			//insert into index struct
		}
		if((strncmp((char *)son->name,"Inode",6)==0)&&(son->children!=NULL)){
			//insert into index struct
		}
		if((strncmp((char *)son->name,"Checksum",9)==0)&&(son->children!=NULL)){
			//insert into index struct
		}
	}
}

#ifdef _WIN32 
static void setAdditionalData(AlertList *l,xmlNode *root){
#else
void setAdditionalData(AlertList *l,xmlNode *root){
#endif
	xmlNode *son;
	for(son = root->CHILDREN; son; son = son->NEXT){
		if((strncmp((char *)son->name,"boolean",8)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"byte",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"character",10)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"date-time",10)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"integer",8)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"ntpstamp",9)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"portlist",9)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"real",5)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"string",7)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"byte-string",12)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
		if((strncmp((char *)son->name,"xml",4)==0)&&(son->children!=NULL)){
			if(son->CONTENT!=NULL){
				//insert into index struct
			}
		}
	}
}

#endif
